package com.iot.broker.mvc.mqtt.config;

import java.util.concurrent.Executors;

import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Session;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

@EnableJms
@Configuration
public class JmsConfig {

	/**
	 * 发布-订阅模式的ListenerContainer
	 * https://blog.csdn.net/pontuss/article/details/83752489
	 * 
	 * @param connectionFactory
	 * @return
	 */
	@Bean(name = "topicListenerFactory")
	public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setPubSubDomain(true);
		factory.setTaskExecutor(Executors.newFixedThreadPool(6));
		factory.setConcurrency("6");
		factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
		factory.setConnectionFactory(connectionFactory);
		return factory;
	}

	/**
	 * P2P模式的ListenerContainer
	 * 
	 * @param connectionFactory
	 * @return
	 */
	@Bean(name = "queueListenerFactory")
	public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		factory.setPubSubDomain(false);
		factory.setTaskExecutor(Executors.newFixedThreadPool(6));
		factory.setConcurrency("6");
		factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
		factory.setConnectionFactory(connectionFactory);
		return factory;
	}
	
	

	@Bean
	public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
		JmsTemplate jmsTemplate = new JmsTemplate();
		// 定义持久化后节点挂掉以后，重启可以继续消费.NON_PERSISTENT 表示非持久化，PERSISTENT 表示持久化
		jmsTemplate.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		jmsTemplate.setConnectionFactory(connectionFactory);
		jmsTemplate.setSessionTransacted(false);
		
//	        Session.AUTO_ACKNOWLEDGE  消息自动签收
//	        Session.CLIENT_ACKNOWLEDGE  客户端调用acknowledge方法手动签收
//	        Session.DUPS_OK_ACKNOWLEDGE 不必必须签收，消息可能会重复发送
		jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);// 客户端签收模式
		
		
		
		//设置创建连接的工厂//JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);//优化连接工厂，这里应用缓存池 连接工厂就即可    JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);

		//设置默认消费topic//jmsTemplate.setDefaultDestination(topic());//设置发布订阅消息类型    jmsTemplate.setPubSubDomain(isPubSubDomain);

		//deliveryMode, priority, timeToLive 的开关，要生效，必须配置为true，默认false    jmsTemplate.setExplicitQosEnabled(true);

		//DeliveryMode.NON_PERSISTENT=1:非持久 ; DeliveryMode.PERSISTENT=2:持久    jmsTemplate.setDeliveryMode(DeliveryMode.PERSISTENT);

		//默认不开启事务    System.out.println("是否开启事务"+jmsTemplate.isSessionTransacted());

		//如果session带有事务，并且事务成功提交，则消息被自动签收。如果事务回滚，则消息会被再次传送。//jmsTemplate.setSessionTransacted(true);//不带事务的session的签收方式，取决于session的配置。//默认消息确认方式为1，即AUTO_ACKNOWLEDGE    System.out.println("是否消息确认方式"+jmsTemplate.getSessionAcknowledgeMode());

		//消息的应答方式，需要手动确认，此时SessionTransacted必须被设置为false，且为Session.CLIENT_ACKNOWLEDGE模式//Session.AUTO_ACKNOWLEDGE  消息自动签收//Session.CLIENT_ACKNOWLEDGE  客户端调用acknowledge方法手动签收//Session.DUPS_OK_ACKNOWLEDGE 不必必须签收，消息可能会重复发送    jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
		
		
		
		
		
		
		
		
		
		
		
		
		
		return jmsTemplate;
	}
}
